<template>
  <div class="editYard">
    <el-row>
      <el-col :span="20">
        <el-form :model="ruleForm"
                 :rules="rules"
                 ref="ruleForm"
                 label-width="120px"
                 class="ruleForm">
          <el-form-item label="堆场名称"
                        prop="yardName">
            <el-input v-model="ruleForm.yardName"
                      class="normal-input"></el-input>
          </el-form-item>
          <el-form-item label="堆场长度(mm)"
                        prop="yardLength">
            <el-input v-model.number="ruleForm.yardLength"
                      class="normal-input"></el-input>
          </el-form-item>
          <el-form-item label="堆场宽度(mm)"
                        prop="yardWidth">
            <el-input v-model.number="ruleForm.yardWidth"
                      class="normal-input"></el-input>
          </el-form-item>
          <el-form-item label="堆场示意图">
            <el-upload class="avatar-uploader"
                       action="https://jsonplaceholder.typicode.com/posts/"
                       :show-file-list="false"
                       :http-request="uploadFile"
                       accept=".jpg,.png,.jpeg">
              <img v-if="imageUrl"
                   :src="imageUrl"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span class="normal-span">支持扩展名：.jpg .png .jpeg</span>
          </el-form-item>
          <el-form-item>
            <el-button @click="cancel">取消</el-button>
            <el-button type="success"
                       @click="submitForm('ruleForm')">保存</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import axios from 'axios'
import qs from 'qs'
import OSS from 'ali-oss'
export default {
  data () {
    return {
      ruleForm: {
        yardName: '',
        yardLength: '',
        yardWidth: '',
        imgUrl: '',
      },
      uuid: this.$route.query.uuid,
      imageUrl: '',
      bucketUrl: '',
      rules: {
        yardName: [
          { required: true, message: '请输入堆场名称', trigger: 'blur' },
        ],
        yardLength: [
          { required: true, message: '请输入堆场长度', trigger: 'blur' },
          { type: 'number', message: '宽度必须为数字值' }
        ],
        yardWidth: [
          { required: true, message: '请输入堆场宽度', trigger: 'blur' },
          { type: 'number', message: '宽度必须为数字值' }
        ],
      }
    }
  },
  methods: {
    getData () {
      axios.get('admin/stack/getStackById', {
        params: {
          id: this.$route.query.id,
          uuid: this.uuid
        },
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
      }
      ).then(res => {
        this.ruleForm.yardName = res.data.data.list.name;
        this.ruleForm.yardLength = Number(res.data.data.list.length);
        this.ruleForm.yardWidth = Number(res.data.data.list.width);
        this.ruleForm.imgUrl = res.data.data.list.img;
        if (res.data.data.list.img != '') {
          this.imageUrl = res.data.data.list.path;
        }

      }, err => {
        console.log(err)
      })
    },
    cancel () {
      this.$router.go(-1)
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post('admin/stack/updateStack', qs.stringify({ id: this.$route.query.id, name: this.ruleForm.yardName, length: this.ruleForm.yardLength, width: this.ruleForm.yardWidth, img: this.ruleForm.imgUrl, uuid: this.uuid }), {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
          }).then(res => {
            if (res.data.code == 1) {
              this.$message({
                message: '修改成功',
                type: 'success'
              });
              this.$router.go(-1);
            } else {
              this.$message.error(res.data.msg);
            }
          }, err => {
            console.log(err)
          })
        } else {
          return false;
        }
      });
    },
    uploadFile (data) {
      var _this = this;
      var client = new OSS({
        region: 'oss-cn-hangzhou',
        accessKeyId: this.util.decrypt(process.env.AES_OSS_ID),
        accessKeySecret: this.util.decrypt(process.env.AES_OSS_SECRET),
        bucket: _this.bucketUrl,
      })
      var address = 'upload/yard/' + new Date().getTime() + Math.floor(Math.random() * 10) + data.file.name;
      client.multipartUpload(address, data.file).then((res) => {
        if (res.res.requestUrls.length > 0) {
          _this.imageUrl = res.res.requestUrls[0].replace(/\?(.*)$/, '');
          _this.ruleForm.imgUrl = (res.name).substr(7)
        }
      }).catch((err) => {
        console.log(err)
      })
    },
  },
  mounted () {
    if (this.$route.query.project_id == undefined) {
      this.rules.yardLength = [];
      this.rules.yardWidth = [];
    }
    this.getData();
    if (location.hostname === 'localhost') {
      this.bucketUrl = 'dev-hibuilding-new'
    } else if (location.hostname === 'dev_front.ihibuilding.cn') {
      this.bucketUrl = 'dev-hibuilding-new'
    } else if (location.hostname === 'test_front.ihibuilding.cn') {
      this.bucketUrl = 'dev-hibuilding-new'
    } else if (location.hostname === 'www.hibuilding.cn') {
      this.bucketUrl = 'hibuilding-new'
    } else if (location.hostname === 'front.hibuilding.cn') {
      this.bucketUrl = 'hibuilding-new'
    }
  }
}
</script>
<style lang="scss" scoped>
.editYard {
  padding: 20px;
  .normal-input {
    width: 300px;
  }
  .normal-span {
    font-size: 12px;
    color: #666;
  }
  .avatar-uploader /deep/ .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader /deep/ .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px !important;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>